package com.jiuzhe.commercial.ui.my;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.jiuzhe.commercial.R;
import com.jiuzhe.commercial.base.BaseActivity;
import com.jiuzhe.commercial.constant.Constants;
import com.jiuzhe.commercial.http.CallBack;
import com.jiuzhe.commercial.http.HttpClient2;
import com.jiuzhe.commercial.utils.SPUtil;
import com.jiuzhe.commercial.utils.UIUtils;

import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by huanghao on 2018/5/25.
 */

public class ZhiFuBaoActivity extends BaseActivity {
    @BindView(R.id.tv_accounts_name)
    EditText tvAccountsName;
    @BindView(R.id.tv_account)
    EditText tvAccount;
    @BindView(R.id.bt_save)
    Button btSave;
    private String mAccountName;
    private String mAccount;

    @Override
    protected void loadViewLayout() {
        setContentView(R.layout.activity_zhifubao);
        setTitle("支付宝帐号");
    }

    @Override
    protected void loadData() {

    }

    @OnClick({R.id.bt_save})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_save:
                saveAccount();
                break;
        }
    }

    private void saveAccount() {
        mAccountName = tvAccountsName.getText().toString().trim();
        mAccount = tvAccount.getText().toString().trim();
        if (TextUtils.isEmpty(mAccountName)){
            UIUtils.showToastSafe("请输入支付宝帐号人名");
            return;
        }
        if (TextUtils.isEmpty(mAccount)){
            UIUtils.showToastSafe("请输入支付宝帐号");
            return;
        }
        String id = SPUtil.getString(ZhiFuBaoActivity.this, Constants.ID);
        String mer_id = SPUtil.getString(ZhiFuBaoActivity.this, Constants.MERCHANTID);
        HashMap<String, String> map = new HashMap<>();
        map.put("user_id",mer_id);
        map.put("channel","alipay");
        map.put("recipient_account",mAccount);
        map.put("recipient_name",mAccountName);
        map.put("recipient_type","b2c");
        map.put("recipient_open_bank_code","");

        String token = SPUtil.getString(ZhiFuBaoActivity.this, Constants.TOKEN);
        String autoSignStr = Constants.SAVESETTLEACCOUNT.substring(Constants.SAVESETTLEACCOUNT.lastIndexOf("0/") + 1)  + "?token=";
        String signStr = UIUtils.Md5(autoSignStr + token);

        map.put("token",id);
        map.put("sign",signStr);

        HttpClient2.post(this, Constants.SAVESETTLEACCOUNT , map, new CallBack<List<String>>() {
            @Override
            public void onSuccess(List<String> stringList) {
                if(stringList.get(0).equals("32")){//结算账户保存成功
                    Intent data = new Intent();
                    data.putExtra("name", mAccountName);
                    data.putExtra("account", mAccount);
                    setResult(400, data);
                    finish();
                }
            }
        });
    }
}
